package com.kuiandjie.web;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.kuiandjie.entity.Contact;
import com.kuiandjie.entity.Employee;
import com.kuiandjie.entity.Log;
import com.kuiandjie.entity.Project;
import com.kuiandjie.service.ConcatService;
import com.kuiandjie.service.LogService;
import com.kuiandjie.util.DateUtil;

public class AddContactServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		Project curp = (Project) session.getAttribute("curp");
		
		if(curp != null) {
			if("POST".equals(request.getMethod())) {
				PrintWriter out = response.getWriter();
				response.setContentType("text/html");
				String name = request.getParameter("name");
				String company = request.getParameter("company");
				String moblie = request.getParameter("moblie");
				String tel = request.getParameter("tel");
				String email = request.getParameter("email");
				String address = request.getParameter("address");
				String website = request.getParameter("website");
				String weibo = request.getParameter("weibo");
				String content = request.getParameter("content");
				
				if(name.trim() == "" || company.trim() == "" || moblie.trim() == "" || tel.trim() == "" || email.trim() == "") {
					out.print("1002");
				} else {
					Employee employee = (Employee) session.getAttribute("employee");
					Contact c = new Contact();
					c.setName(name);
					c.setCompanyname(company);
					c.setMoblie(moblie);
					c.setTel(tel);
					c.setEmail(email);
					c.setAddress(address);
					c.setWebsite(website);
					c.setWeibo(weibo);
					c.setContent(content);
					c.setCreatetime(DateUtil.getNow());
					c.setProjectid(curp.getId());
					int id = new ConcatService().insertContact(c);
					
					if(id != -1) {
						Log log = new Log();
						log.setDesc("添加了联系人<a href='contact.jspx'>" + name + "</a>");
						log.setEmployeeId(employee.getId());
						log.setProjectId(curp.getId());
						log.setTime(c.getCreatetime());
						log.setName(employee.getName());
						log.setPic(employee.getPic());
						
						LogService ls = new LogService();
						ls.inserLog(log);
						
						request.setAttribute("msg", new Gson().toJson(log));
						request.getRequestDispatcher("msgflow.jspx").include(request, response);
						out.print("1001");
					} else {
						out.print("1002");
					}
				}
				
				out.flush();
				out.close();
			} else {
				request.getRequestDispatcher("WEB-INF/views/addcontact.jsp").forward(request, response);
			}
		}
	}
}
